import { message } from 'antd';

const showError = (txt) => {
  // window.ELEMENT.Message({
  //   showClose: true,
  //   message: txt,
  //   type: 'error',
  // });
};

class PluginFactory {
  name = 'plugin-factory';

  constructor() {
    window.Asc.plugin.init = function () {
      // docEditor.serviceCommand("commond start");
      // window.parent.Comment.Gateway.on('internalcommand', function (data) {
      //   console.log(data.command);
      // });
    };
    window.Asc.plugin.onCommandCallback = function (params) {
      // const plugin = window.Asc.plugin;
      console.log('onCommandCallback', params);
    };
    console.log('constructor', window.Asc);
  }
  //
  test() {
    window.Asc.plugin.executeMethod('GetCurrentContentControl', [], function (data) {
      // debugger;
    });
    window.Asc.plugin.callCommand(
      () => {
        const oDocument = Api.GetDocument();
        // const oRange = oDocument.GetRangeBySelect();
        // oRange.AddBookmark('BookmarTest');
        // const book = oDocument.GetBookmarkRange('BookmarTest');
        // oDocument.GetBookmarkRange("Bookmark222");
        const aBookmarks = oDocument.GetAllBookmarksNames();
        const oRange = oDocument.GetBookmarkRange('c1');
        const txt = oRange.Text;
        // debugger
        // oRange.Delete();
        // oRange.AddText('【${txt}】');
        // // oRange.WrapInMailMergeField();
        // // console.log(oRange)
        // oRange.AddText("【哈啊哈】",'before');
        // // oRange.AddText("】",'after');
        // Api.ReplaceTextSmart([txt], "[---HHAA---]", "");
        oDocument.SearchAndReplace({ searchString: txt, replaceString: '[---HHAA---]' });
        oRange.SetBold(true);
        // oRange.SetHighlight("gray");
        oRange.Select();

        // oDocument
        // const bookStr = JSON.stringify(aBookmarks);
        // console.log('AddBookmark finish.');
        // return book;
      },
      false,
      true,
      // (txt) => {
      //   console.log('AddBookmark finish.');
      //   // debugger;
      // },
    );
  }

  addUrlLink = () => {
    window.Asc.plugin.callCommand(() => {
      const oDocument = Api.GetDocument();
      const oParagraph = Api.CreateParagraph();
      const oHyperlink = Api.CreateHyperlink('https://bing.com', '测试 链接');
      oParagraph.AddElement(oHyperlink);
      // oDocument.Push(oParagraph);
      oDocument.InsertContent([oParagraph]);
    });
  };

  getCurrentTxt = async () => {
    return new Promise<string>((resolve, reject) => {
      window.Asc.plugin.executeMethod(
        'GetSelectedText',
        [
          {
            Numbering: false,
            Math: false,
            TableCellSeparator: '\n',
            ParaSeparator: '\n',
            TabSymbol: String.fromCharCode(9),
          },
        ],
        function (data) {
          resolve(data);
        },
      );
    });
  };

  // 添加标题/选项
  addBookmarkWithTitle = (code: string) => {
    return new Promise<string>((resolve, reject) => {
      window.Asc.scope.code = code;
      window.Asc.plugin.callCommand(
        () => {
          try {
            const titleCode = Asc.scope.code;
            const oDocument = Api.GetDocument();
            const oRange = oDocument.GetRangeBySelect();
            oRange.AddBookmark(titleCode);
            console.log('Asc.plugin.callCommand', 'AddBookmark', titleCode, oRange);
            // debugger;
            // oDocument.SearchAndReplace({ searchString: txt, replaceString: `[${txt}]` });
            // oRange.SetBold(true);
            // oRange.Select();
            return 'success';
          } catch (error) {
            return error.message;
          }
        },
        false,
        false,
        (status) => {
          console.log('status', status);
          if (status === 'success') {
            resolve(status);
          }
          if (status === 'error') {
            message.error(status);
            reject('书签创建失败');
          }
        },
      );
    });
  };

  // 删除书签
  removeBookmark = (codes: string[]) => {
    return new Promise<string>((resolve, reject) => {
      window.Asc.scope.removeCodes = codes;
      window.Asc.plugin.callCommand(
        () => {
          try {
            const titleCode = Asc.scope.removeCodes;
            const oDocument = Api.GetDocument();
            titleCode?.map((v) => {
              oDocument.DeleteBookmark(v);
              console.log('删除书签', v);
            });
            return 'success';
          } catch (error) {
            return error.message;
          }
        },
        false,
        false,
        (status) => {
          console.log('status', status);
          if (status === 'success') {
            resolve(status);
          }
          if (status === 'error') {
            message.error(status);
            reject('书签删除失败');
          }
        },
      );
    });
  };

  // 跳转书签
  jumpToBookMark = (code: string) => {
    console.log('jumpToBookMark', code);
    window.Asc.scope.jumpCode = code;
    window.Asc.plugin.callCommand(
      () => {
        try {
          const oDocument = Api.GetDocument();
          const oRange = oDocument.GetBookmarkRange(Asc.scope.jumpCode);
          oRange.Select();
          return 'success';
        } catch (error) {
          return error.message;
        }
      },
      false,
      true,
      (txt) => {
        if (txt !== 'success') {
          message.error(txt);
        }
      },
    );
  };

  //  数据项目
  addEventData = async (name: string) => {
    return new Promise((resolve, reject) => {
      window.Asc.scope.eventDataName = name;
      window.Asc.plugin.callCommand(
        function () {
          try {
            const oDocument = Api.GetDocument();
            const txt = Asc.scope.eventDataName;
            const oParagraph = Api.CreateParagraph();
            oParagraph.AddText(txt);
            oParagraph.SetBold(true);
            oParagraph.SetFontSize(16);
            oDocument.InsertContent([oParagraph], true);
            return 'success';
          } catch (error) {
            return error.message || '未知错误';
          }
        },
        false,
        true,
        (txt) => {
          console.log('addEventData', txt);
          if (txt === 'success') {
            resolve('');
            return;
          }
          message.error(txt);
          reject('');
        },
      );
    });
  };
  //  添加文件链接
  addUrlLinkWithFile = (url: string, title: string) => {
    return new Promise((resolve, reject) => {
      window.Asc.scope.addUrlLinkWithFileUrl = url;
      window.Asc.scope.addUrlLinkWithFileTitle = title;
      window.Asc.plugin.callCommand(
        function () {
          try {
            const url = Asc.scope.addUrlLinkWithFileUrl;
            const title = Asc.scope.addUrlLinkWithFileTitle;
            const oDocument = Api.GetDocument();
            const oParagraph = Api.CreateParagraph();
            const oHyperlink = Api.CreateHyperlink(url, title);
            oParagraph.AddElement(oHyperlink);
            oDocument.InsertContent([oParagraph]);
            return 'success';
          } catch (error) {
            return error.message || '未知错误';
          }
        },
        false,
        true,
        (txt) => {
          console.log('addEventData', txt);
          if (txt === 'success') {
            resolve('');
            return;
          }
          message.error(txt);
          reject('');
        },
      );
    });
  };

  addOfficeItem() {}

  addOfficeItemSelect() {}

  addOfficeItemInput() {}

  addOfficeItemDescribe() {}
}

export { PluginFactory };
